package hu.genius.view.utils;

import hu.genius.R;
import hu.genius.model.BasketManager;
import hu.genius.model.entity.Product;
import hu.genius.view.SelectProduct;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map.Entry;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ViewUtils {

	@Deprecated
	public static View buildProductsView(final Context context,
			List<Product> productList) {

		LinearLayout actionLayout = new LinearLayout(context);
		actionLayout.setOrientation(LinearLayout.VERTICAL);
		actionLayout.setBackgroundColor(Color.WHITE);

		for (final Product product : productList) {
			LinearLayout rowLayout = new LinearLayout(context);
			rowLayout.setOrientation(LinearLayout.HORIZONTAL);

			ImageView productView = new ImageView(context);
			productView.setImageBitmap(loadImage(context, product.getId()
					+ "s110c" + product.getPrice() + ".00i"));
			productView.setLayoutParams(new LinearLayout.LayoutParams(
					getDpSize(context, 110), getDpSize(context, 110)));
			rowLayout.addView(productView);

			LinearLayout infoLayout = new LinearLayout(context);
			infoLayout.setOrientation(LinearLayout.HORIZONTAL);

			LinearLayout textLayout = new LinearLayout(context);
			textLayout.setOrientation(LinearLayout.VERTICAL);

			TextView nameView = new TextView(context);
			nameView.setText(product.getName());
			nameView.setTextColor(Color.BLACK);
			textLayout.addView(nameView);

			TextView priceView = new TextView(context);
			priceView.setText(product.getPrice() + "");
			priceView.setTextColor(Color.BLACK);
			textLayout.addView(priceView);

			infoLayout.addView(textLayout);

			Button selectButton = new Button(context);
			selectButton.setBackgroundResource(R.drawable.small_basket);
			selectButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent(context, SelectProduct.class);
					intent.putExtra("product_id", product.getId());
					intent.putExtra("product_category_id",
							product.getCategoryId());
					intent.putExtra("product_name", product.getName());
					intent.putExtra("product_price", product.getPrice());
					context.startActivity(intent);
					Log.d("zs", "selected product: (" + product + ")");
				}
			});
			infoLayout.addView(selectButton);

			rowLayout.addView(infoLayout);

			actionLayout.addView(rowLayout);
		}

		return actionLayout;
	}

	@Deprecated
	private static View buildBasketView(final Context context,
			final BasketManager basketManager) {
		Log.i("zs", "Show basket " + basketManager.toString());

		LinearLayout actionLayout = new LinearLayout(context);
		actionLayout.setOrientation(LinearLayout.VERTICAL);
		actionLayout.setBackgroundColor(Color.WHITE);

		for (Entry<Product, Integer> entry : basketManager.getOrderMap()
				.entrySet()) {
			Product product = entry.getKey();

			LinearLayout rowLayout = new LinearLayout(context);
			rowLayout.setOrientation(LinearLayout.HORIZONTAL);

			ImageView productView = new ImageView(context);
			productView.setImageBitmap(loadImage(context, product.getId()
					+ "s110c" + product.getPrice() + ".00i"));
			productView.setLayoutParams(new LinearLayout.LayoutParams(
					getDpSize(context, 110), getDpSize(context, 110)));
			rowLayout.addView(productView);

			LinearLayout infoLayout = new LinearLayout(context);
			infoLayout.setOrientation(LinearLayout.HORIZONTAL);

			LinearLayout textLayout = new LinearLayout(context);
			textLayout.setOrientation(LinearLayout.VERTICAL);

			TextView nameView = new TextView(context);
			nameView.setText(product.getName());
			nameView.setTextColor(Color.BLACK);
			textLayout.addView(nameView);

			TextView priceView = new TextView(context);
			priceView.setText(product.getPrice() + "");
			priceView.setTextColor(Color.BLACK);
			textLayout.addView(priceView);

			infoLayout.addView(textLayout);

			rowLayout.addView(infoLayout);
			actionLayout.addView(rowLayout);
		}

		return actionLayout;
	}

	@Deprecated
	public static ImageView setupProductImageView(Context context,
			ImageView imageView, Product product) {
		if (context != null || product != null) {
			Bitmap bitmap = loadImage(context, product.getId() + "s110c"
					+ product.getPrice() + ".00i");
			imageView.setImageBitmap(bitmap);
			imageView.setLayoutParams(new LinearLayout.LayoutParams(getDpSize(
					context, 110), getDpSize(context, 110)));
			return imageView;
		} else {
			return null;
		}
	}

	@Deprecated
	public static ImageView createProductImageView(Context context,
			Product product) {
		if (context != null || product != null) {
			ImageView productView = new ImageView(context);
			productView.setImageBitmap(loadImage(context, product.getId()
					+ "s110c" + product.getPrice() + ".00i"));
			productView.setLayoutParams(new LinearLayout.LayoutParams(
					getDpSize(context, 110), getDpSize(context, 110)));
			return productView;
		} else {
			return null;
		}
	}

	public static Bitmap loadImage(Context context, String source) {
		try {
			InputStream is = context.getAssets().open(
					"images/" + source + ".jpg");
			return BitmapFactory.decodeStream(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int getDpSize(Context context, float dp) {
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		int pixels = (int) (metrics.density * dp + 0.5f);
		return pixels;
	}
	
//	public int getDpSize(float dp) {
//		DisplayMetrics metrics = this.getResources().getDisplayMetrics();
//		int pixels = (int) (metrics.density * dp + 0.5f);
//		return pixels;
//	}
	
	public static Bitmap downloadImage(String url) {
		URL myFileUrl = null;
		try {
			myFileUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) myFileUrl
					.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();

			return BitmapFactory.decodeStream(is);
		} catch (MalformedURLException e) {
			Log.e("m", e.getMessage());
		} catch (IOException e) {
			Log.e("m", e.getMessage());
		}
		return null;
	}
}
